package com.ty.tour.controller;


import com.ty.tour.common.utils.ReturnData;
import com.ty.tour.pojo.Orders;
import com.ty.tour.pojo.UserInfo;
import com.ty.tour.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/***
 * @Description: 订单Controller
 * @Author: xiexiaoping
 * @Date: 2020/3/21 10:43
 **/
@Api(value = "订单Controller", tags = "订单查看")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * @MethodName: userOrder
     * @Description: 用户查看订单
     * @Param: [userId]
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/21 10:48
     **/
    @PostMapping("/user-order")
    @ApiOperation(value = "用户查看订单", notes = "订单实体")
    @ApiImplicitParam(value = "用户编号", name = "userId", dataType = "Integer", paramType = "query", required = true)
    public ReturnData userOrder(HttpSession session) {
        final ReturnData returnData = new ReturnData();
        try {
            Object obj = session.getAttribute("user");
            if (obj == null) {
                return returnData.fail("您还没有登录，请前往登录");
            }
            UserInfo user = (UserInfo) obj;
            List<Orders> orders = orderService.userOrder(user.getId());
            return returnData.success(orders, 1);
        } catch (Exception e) {
            e.printStackTrace();
            return returnData.fail();
        }
    }

    /***
     * @MethodName: saveSingleOrder
     * @Description: 保存旅游订单
     * @Param: []
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/21 10:58
     **/
    @PostMapping("save-order")
    @ApiOperation(value = "保存旅游订单", notes = "订单实体")
    public ReturnData saveOrder(HttpSession session, @RequestBody Orders order) {
        final ReturnData returnData = new ReturnData();
        Object obj = session.getAttribute("user");
        if (obj == null) {
            return returnData.fail("您还没有登录，请前往登录");
        }
        UserInfo user = (UserInfo) obj;
        order.setUserId(user.getId());//存入用户编号
        order.setUsername(user.getName());//存入用户姓名
        if (order == null) {
            return returnData.fail();
        }
        return orderService.saves(order) == true ? returnData.statusTool(1) : returnData.fail();
    }

    /***
     * @MethodName: deleteOrder
     * @Description: 删除订单
     * @Param: []
     * @Return: com.ty.tour.common.utils.ReturnData
     * @Author: xiexiaoping
     * @Date: 2020/3/21 11:15
     **/
    @PostMapping("delete-order")
    @ApiOperation(value = "删除订单", notes = "订单实体")
    public ReturnData deleteOrder(HttpSession session, Integer orderId) {
        final ReturnData returnData = new ReturnData();
        Object obj = session.getAttribute("user");
        if (obj == null) {
            return returnData.fail("您还没有登录，请前往登录");
        }
        if (orderId == null) {
            return returnData.fail();
        }
        Orders order = orderService.getById(orderId);
        if (order == null) {
            return returnData.fail();
        }
        return orderService.deleteById(order.getId()) > 0 ? returnData.statusTool(1) : returnData.fail();
    }
}
